﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace Handsfree
{
    public partial class HomeForm : Form
    {
        #region Global Variables
        Preferences preferences = new Preferences();
        SpeechRecognition s = new SpeechRecognition();
        UDPReceive u = new UDPReceive();
        Process process;
        #endregion

        public HomeForm()
        {
            InitializeComponent();
            if (preferences.onStartup())
             start_Click(new Object(), new EventArgs());
        }

        #region Methods

        //Starts SpeechRecognizer
        private void StartSpeech()
        {
           s.StartEngine();
            startSpeech.Text = "Stop Speech Recognition";
        }
        //Start Head Tracking
        //Calls external application
        private void StartHeadTracking()
        {
            /*process = new Process();
            process.StartInfo.FileName = @""; //Path to streamer.exe
            process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
            process.Start();*/
            u.StartEngine();
            startCam.Text = "Stop Head Tracking";
        }
        //Stop SpeechRecognizer
        private void StopSpeech()
        {
            s.StopEngine();
            startSpeech.Text = "Start Speech Recognition";
        }
        //Stop Head Tracking
        //Exits application
        private void StopHeadTracking()
        {
            u.StopEngine();
            process.Kill();
            startCam.Text = "Start Head Tracking";
        }
        #endregion

        #region Event Handlers for Form Elements

        private void start_Click(object sender, EventArgs e)
        {
            StartHeadTracking();
            StartSpeech();
            start.Enabled = false;
            start.Text = "Running..";
        }

        private void startCam_Click(object sender, EventArgs e)
        {
            if (startCam.Text == "Start Head Tracking") 
                StartHeadTracking();
            else
                StopHeadTracking();
        }

        private void startSpeech_Click(object sender, EventArgs e)
        {
            if (startSpeech.Text == "Start Speech Recognition")
                StartSpeech();
            else
                StopSpeech();
        }

        private void resToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StopSpeech();
            StopHeadTracking();
            StartHeadTracking();
            StartSpeech();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StopSpeech();
            StopHeadTracking();
            start.Enabled = true;
            start.Text = "Start Application";
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StopSpeech();
            StopHeadTracking();
            Application.Exit();
        }

        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            preferences.Show();
        }

        private void newCommandsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Add_New newCommand = new Add_New();
            newCommand.Show();
        }

        #endregion

        private void viewCommandsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List l = new List();
            l.Show();
        }

        
        
    }
}
